﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace LibarayMS
{
    public partial class Frm_pub_manage : Form
    {
        public event myDelegate returnEvent;
        public event myDelegate logoutEvent;
        private string mname;
        public Frm_pub_manage(string mname)
        {
            this.mname = mname;
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            returnEvent();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            logoutEvent();
            this.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        protected void bind_pub_info()
        {
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = "select * from edit_house_info;";
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["PHID"].ToString());
                myitem.SubItems.Add(dr["EName"].ToString());
                myitem.SubItems.Add(dr["contacts"].ToString());
                myitem.SubItems.Add(dr["tel"].ToString());
                myitem.SubItems.Add(dr["email"].ToString());
                myitem.SubItems.Add(dr["address"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void Frm_pub_manage_Load(object sender, EventArgs e)
        {
            lbl_mname.Text = mname;
            bind_pub_info();
        }

        protected void clearTextbox()
        {
            txt_EName.Text = "";
            txt_contacts.Text = "";
            txt_tel.Text = "";
            txt_email.Text = "";
            txt_address.Text = "";
            lbl_status.Text = "添加";
        }

        private void btb_save_Click(object sender, EventArgs e)
        {
            string ename=txt_EName.Text.Trim();
            string tel=txt_tel.Text.Trim();
            string contacts=txt_contacts.Text.Trim();
            string email=txt_email.Text.Trim();
            string address=txt_address.Text.Trim();
            if (ename == "")
            {
                lbl_note.Text = "出版社名称不能为空！";
                lbl_note.ForeColor= Color.Red;
                txt_EName.Focus();
            }
            else if (lbl_status.Text == "添加")
            {
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                string str = string.Format("insert into edit_house_info values(null,'{0}','{1}','{2}','{3}','{4}');", ename, contacts, tel, email, address);
                MySqlCommand cmd= new MySqlCommand(str, conn);
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                if(i>0)
                {
                    lbl_note.Text = "恭喜你，添加成功！";
                    lbl_note.ForeColor = Color.Blue;
                    bind_pub_info();
                    clearTextbox();
                }
                else
                {
                    lbl_note.Text = "对不起，添加失败！";
                    lbl_note.ForeColor = Color.Red;
                }
            }
            else
            {
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                int PHID = int.Parse(listView1.SelectedItems[0].SubItems[0].Text);
                string str = string.Format("update edit_house_info set EName='{0}',contacts='{1}',tel='{2}',email='{3}',address='{4}' where PHID={5};", ename, contacts, tel, email, address, PHID);
                MySqlCommand cmd = new MySqlCommand(str, conn);
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                if(i>0)
                {
                    lbl_note.Text = "恭喜你，修改成功！";
                    lbl_note.ForeColor = Color.Blue;
                    bind_pub_info();
                    clearTextbox();
                }
                else
                {
                    lbl_note.Text = "对不起，修改失败！";
                    lbl_note.ForeColor = Color.Red;
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem myitem = listView1.SelectedItems[0];
                txt_EName.Text = myitem.SubItems[1].Text;
                txt_contacts.Text = myitem.SubItems[2].Text;
                txt_tel.Text = myitem.SubItems[3].Text;
                txt_email.Text = myitem.SubItems[4].Text;
                txt_address.Text = myitem.SubItems[5].Text;
                lbl_status.Text = "修改";
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择要删除的出版社！");
            }
            else
            {
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                int PHID = int.Parse(listView1.SelectedItems[0].SubItems[0].Text);
                string str = string.Format("delete from edit_house_info where PHID={0};", PHID);
                MySqlCommand cmd = new MySqlCommand(str, conn);
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                if (i > 0)
                {
                    lbl_note.Text = "恭喜你，删除成功！";
                    lbl_note.ForeColor = Color.Blue;
                    bind_pub_info();
                    clearTextbox();
                }
                else
                {
                    lbl_note.Text = "对不起，删除失败！";
                    lbl_note.ForeColor = Color.Red;
                }
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            clearTextbox();
            lbl_note.Text = "";
        }
    }
}
